package com.allqj.sfx.ddd.member.infrastructure.repository;

import com.allqj.sfx.ddd.member.domain.entity.flow.FlowType;
import com.allqj.sfx.ddd.member.domain.repository.FlowTypeRepository;
import com.allqj.sfx.ddd.member.infrastructure.converter.FlowTypeConverter;
import com.allqj.sfx.ddd.member.infrastructure.dao.FlowTypeDAO;
import com.allqj.sfx.ddd.member.infrastructure.po.FlowTypeDO;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sjw
 * @className FlowTypeRepositoryImpl
 * @date 2022/7/22 16:31
 * @description: FlowTypeRepository 具体实现
 * Repository接口定义位于domain层
 * Repository接口具体实现位于infrastructure层
 * 与底层db解耦
 */
@Repository
public class FlowTypeRepositoryImpl implements FlowTypeRepository {

    private final FlowTypeDAO dao;

    public FlowTypeRepositoryImpl(FlowTypeDAO dao) {
        this.dao = dao;
    }

    @Override
    public List<FlowType> flowTypes(Integer moduleId) {
        List<FlowTypeDO> flowTypeDOS = dao.findAllByModuleIdAndIsdeleteFalse(moduleId);
        if (flowTypeDOS == null) return new ArrayList<>();
        return flowTypeDOS.stream().map(flowTypeDO -> FlowTypeConverter.INSTANCE.do2Entity(flowTypeDO)).collect(Collectors.toList());
    }

    @Override
    public FlowType flowType(Integer code) {
        FlowTypeDO flowTypeDO = dao.findByCodeAndIsdeleteFalse(code);
        return FlowTypeConverter.INSTANCE.do2Entity(flowTypeDO);
    }
}
